package com.education.teach.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.component.DaggerTimetableComponent;
import com.education.teach.di.module.TimetableModule;
import com.education.teach.mvp.contract.TimetableContract;
import com.education.teach.mvp.model.bean.TimetableBean;
import com.education.teach.mvp.model.service.ApiService;
import com.education.teach.mvp.presenter.TimetablePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.integration.lifecycle.Lifecycleable;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dn.easy.common.constant.Contans;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import static com.jess.arms.utils.Preconditions.checkNotNull;

// 课程表   需要完善 完全木有扩展性
public class TimetableActivity extends BaseActivity<TimetablePresenter> implements TimetableContract.View {

    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.tv_base)
    TextView mTvBase;
    @BindView(R.id.tv_week1)
    TextView mTvWeek1;
    @BindView(R.id.tv_week2)
    TextView mTvWeek2;
    @BindView(R.id.tv_week3)
    TextView mTvWeek3;
    @BindView(R.id.tv_week4)
    TextView mTvWeek4;
    @BindView(R.id.tv_week5)
    TextView mTvWeek5;
    @BindView(R.id.tv_base1)
    TextView mTvBase1;
    @BindView(R.id.tv_week1_1)
    TextView mTvWeek11;
    @BindView(R.id.tv_week2_1)
    TextView mTvWeek21;
    @BindView(R.id.tv_week3_1)
    TextView mTvWeek31;
    @BindView(R.id.tv_week4_1)
    TextView mTvWeek41;
    @BindView(R.id.tv_week5_1)
    TextView mTvWeek51;
    @BindView(R.id.tv_base2)
    TextView mTvBase2;
    @BindView(R.id.tv_week1_2)
    TextView mTvWeek12;
    @BindView(R.id.tv_week2_2)
    TextView mTvWeek22;
    @BindView(R.id.tv_week3_2)
    TextView mTvWeek32;
    @BindView(R.id.tv_week4_2)
    TextView mTvWeek42;
    @BindView(R.id.tv_week5_2)
    TextView mTvWeek52;
    @BindView(R.id.tv_base3)
    TextView mTvBase3;
    @BindView(R.id.tv_week1_3)
    TextView mTvWeek13;
    @BindView(R.id.tv_week2_3)
    TextView mTvWeek23;
    @BindView(R.id.tv_week3_3)
    TextView mTvWeek33;
    @BindView(R.id.tv_week4_3)
    TextView mTvWeek43;
    @BindView(R.id.tv_week5_3)
    TextView mTvWeek53;
    @BindView(R.id.tv_base4)
    TextView mTvBase4;
    @BindView(R.id.tv_week1_4)
    TextView mTvWeek14;
    @BindView(R.id.tv_week2_4)
    TextView mTvWeek24;
    @BindView(R.id.tv_week3_4)
    TextView mTvWeek34;
    @BindView(R.id.tv_week4_4)
    TextView mTvWeek44;
    @BindView(R.id.tv_week5_4)
    TextView mTvWeek54;
    @BindView(R.id.tv_base5)
    TextView mTvBase5;
    @BindView(R.id.tv_week1_5)
    TextView mTvWeek15;
    @BindView(R.id.tv_week2_5)
    TextView mTvWeek25;
    @BindView(R.id.tv_week3_5)
    TextView mTvWeek35;
    @BindView(R.id.tv_week4_5)
    TextView mTvWeek45;
    @BindView(R.id.tv_week5_5)
    TextView mTvWeek55;
    @BindView(R.id.tv_base36)
    TextView mTvBase36;
    @BindView(R.id.tv_week1_6)
    TextView mTvWeek16;
    @BindView(R.id.tv_week2_6)
    TextView mTvWeek26;
    @BindView(R.id.tv_week3_6)
    TextView mTvWeek36;
    @BindView(R.id.tv_week4_6)
    TextView mTvWeek46;
    @BindView(R.id.tv_week5_6)
    TextView mTvWeek56;
    @BindView(R.id.tv_base7)
    TextView mTvBase7;
    @BindView(R.id.tv_week1_7)
    TextView mTvWeek17;
    @BindView(R.id.tv_week2_7)
    TextView mTvWeek27;
    @BindView(R.id.tv_week3_7)
    TextView mTvWeek37;
    @BindView(R.id.tv_week4_7)
    TextView mTvWeek47;
    @BindView(R.id.tv_week5_7)
    TextView mTvWeek57;
    @BindView(R.id.tv_base8)
    TextView mTvBase8;
    @BindView(R.id.tv_week1_8)
    TextView mTvWeek18;
    @BindView(R.id.tv_week2_8)
    TextView mTvWeek28;
    @BindView(R.id.tv_week3_8)
    TextView mTvWeek38;
    @BindView(R.id.tv_week4_8)
    TextView mTvWeek48;
    @BindView(R.id.tv_week5_8)
    TextView mTvWeek58;

    @Inject
    IRepositoryManager mIRepositoryManager;
    @Inject
    RxErrorHandler mRxErrorHandler;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerTimetableComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .timetableModule(new TimetableModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_timetable; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("课程表");
        mToolbar.setToolbarLeftBackImageRes(R.mipmap.top_back);
        mToolbar.setOnLeftTitleClickListener(view -> finish());
        Map<String, String> map = new HashMap<>();
        map.put("clazzId", "61");

        mIRepositoryManager
                .obtainRetrofitService(ApiService.class)
                .findStudentCourseTable(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<TimetableBean>(mRxErrorHandler) {
                    @Override
                    public void onNext(TimetableBean timetableBean) {
                        List<TimetableBean.ResultBean> list = timetableBean.getResult();
                        if (list == null || list.size() == 0)
                            return;
                        mTextView.setText(list.get(0).getGradeClazzName());
                        for (TimetableBean.ResultBean bean : list) {
                            int week = bean.getWeek();
                            int num = bean.getNum();
                            switch (week) {
                                case 1:
                                    if (1 == num)
                                        mTvWeek11.setText(bean.getTitle());

                                    if (2 == num)
                                        mTvWeek12.setText(bean.getTitle());

                                    if (3 == num)
                                        mTvWeek13.setText(bean.getTitle());

                                    if (4 == num)
                                        mTvWeek14.setText(bean.getTitle());

                                    if (5 == num)
                                        mTvWeek15.setText(bean.getTitle());

                                    if (6 == num)
                                        mTvWeek16.setText(bean.getTitle());

                                    if (7 == num)
                                        mTvWeek17.setText(bean.getTitle());

                                    if (8 == num)
                                        mTvWeek18.setText(bean.getTitle());

                                    break;
                                case 2:
                                    if (1 == num)
                                        mTvWeek21.setText(bean.getTitle());

                                    if (2 == num)
                                        mTvWeek22.setText(bean.getTitle());

                                    if (3 == num)
                                        mTvWeek23.setText(bean.getTitle());

                                    if (4 == num) {
                                        mTvWeek24.setText(bean.getTitle());
                                    }
                                    if (5 == num)
                                        mTvWeek25.setText(bean.getTitle());

                                    if (6 == num)
                                        mTvWeek26.setText(bean.getTitle());

                                    if (7 == num)
                                        mTvWeek27.setText(bean.getTitle());

                                    if (8 == num)
                                        mTvWeek28.setText(bean.getTitle());

                                    break;
                                case 3:
                                    if (1 == num)
                                        mTvWeek31.setText(bean.getTitle());

                                    if (2 == num)
                                        mTvWeek32.setText(bean.getTitle());

                                    if (3 == num)
                                        mTvWeek33.setText(bean.getTitle());

                                    if (4 == num)
                                        mTvWeek34.setText(bean.getTitle());

                                    if (5 == num)
                                        mTvWeek35.setText(bean.getTitle());

                                    if (6 == num)
                                        mTvWeek36.setText(bean.getTitle());

                                    if (7 == num)
                                        mTvWeek37.setText(bean.getTitle());

                                    if (8 == num)
                                        mTvWeek38.setText(bean.getTitle());

                                    break;
                                case 4:
                                    if (1 == num)
                                        mTvWeek41.setText(bean.getTitle());

                                    if (2 == num)
                                        mTvWeek42.setText(bean.getTitle());

                                    if (3 == num)
                                        mTvWeek43.setText(bean.getTitle());

                                    if (4 == num)
                                        mTvWeek44.setText(bean.getTitle());

                                    if (5 == num)
                                        mTvWeek45.setText(bean.getTitle());

                                    if (6 == num)
                                        mTvWeek46.setText(bean.getTitle());

                                    if (7 == num)
                                        mTvWeek47.setText(bean.getTitle());

                                    if (8 == num)
                                        mTvWeek48.setText(bean.getTitle());

                                    break;
                                case 5:
                                    if (1 == num)
                                        mTvWeek51.setText(bean.getTitle());

                                    if (2 == num)
                                        mTvWeek52.setText(bean.getTitle());

                                    if (3 == num)
                                        mTvWeek53.setText(bean.getTitle());

                                    if (4 == num)
                                        mTvWeek54.setText(bean.getTitle());

                                    if (5 == num)
                                        mTvWeek55.setText(bean.getTitle());

                                    if (6 == num)
                                        mTvWeek56.setText(bean.getTitle());

                                    if (7 == num)
                                        mTvWeek57.setText(bean.getTitle());

                                    if (8 == num)
                                        mTvWeek58.setText(bean.getTitle());

                                    break;
                            }

                        }

                    }
                });
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

}
